package sd.qd.xiaoming.coffeedesk.service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sd.qd.xiaoming.coffeedesk.dao.CdDictDAO;
import sd.qd.xiaoming.coffeedesk.dao.CdDirectoryDAO;
import sd.qd.xiaoming.coffeedesk.dao.CdFileDAO;
import sd.qd.xiaoming.coffeedesk.dao.CdIconDAO;
import sd.qd.xiaoming.coffeedesk.dao.CdIconListDAO;
import sd.qd.xiaoming.coffeedesk.dao.CdNoteDAO;
import sd.qd.xiaoming.coffeedesk.dao.CdNoteTypeDAO;
import sd.qd.xiaoming.coffeedesk.dao.CdShareAccountDAO;
import sd.qd.xiaoming.coffeedesk.entity.CdDict;
import sd.qd.xiaoming.coffeedesk.entity.CdDirectory;
import sd.qd.xiaoming.coffeedesk.entity.CdFile;
import sd.qd.xiaoming.coffeedesk.entity.CdIcon;
import sd.qd.xiaoming.coffeedesk.entity.CdIconList;
import sd.qd.xiaoming.coffeedesk.entity.CdNote;
import sd.qd.xiaoming.coffeedesk.entity.CdNoteType;
import sd.qd.xiaoming.coffeedesk.entity.CdShareAccount;
import sd.qd.xiaoming.coffeedesk.support.FileSupport;

@Transactional
@Service
public class InitDBService {

	@Autowired
	private CdDictDAO cdDictDAO;
	@Autowired
	private CdDirectoryDAO cdDirectoryDAO;
	@Autowired
	private CdFileDAO cdFileDAO;
	@Autowired
	private CdIconDAO cdIconDAO;
	@Autowired
	private CdIconListDAO cdIconListDAO;
	@Autowired
	private CdNoteDAO cdNoteDAO;
	@Autowired
	private CdNoteTypeDAO cdNoteTypeDAO;
	@Autowired
	private CdShareAccountDAO cdShareAccountDAO;
    @Autowired
    private FileSupport fileSupport;

	public void initDatabase() {
		System.out.print("CoffeeDesk>>> ");
		System.out.print("正在初始化数据库");
		System.out.println();

		//数据字典
		List<CdDict> oldDics = this.cdDictDAO.findAll();
		this.cdDictDAO.delete(oldDics.toArray(new CdDict[oldDics.size()]));
		CdDict d1 = new CdDict();
		d1.setCode("app_name");
		d1.setName("Coffee Desk");
		CdDict d2 = new CdDict();
		d2.setCode("app_author");
		d2.setName("Li Xiaoming");
        CdDict username = new CdDict();
        username.setCode("username");
        username.setName("admin");
        CdDict pwd = new CdDict();
        pwd.setCode("pwd");
        pwd.setName("cd");
        this.cdDictDAO.save(d1, d2, username, pwd);

		//目录
		List<CdDirectory> oldDir = this.cdDirectoryDAO.findAll();
		this.cdDirectoryDAO.delete(oldDir.toArray(new CdDirectory[oldDir.size()]));
		CdDirectory dir1 = new CdDirectory();
		dir1.setName("根目录");
		dir1.setEditTime(new Date());
		dir1=this.cdDirectoryDAO.save(dir1);

		//文件
		List<CdFile> files = this.cdFileDAO.findAll();
		this.cdFileDAO.delete(files.toArray(new CdFile[files.size()]));
        for (CdFile f : files) {
            this.fileSupport.delete(f.getCode());
        }
		CdFile cf = new CdFile();
		cf.setAccessCount(0L);
		cf.setCode(UUID.randomUUID().toString());
		cf.setDirectory(dir1);
		cf.setEditTime(new Date());
		cf.setName("我的第一个文件.txt");
		cf.setSize(4L);
		cf.setSummary("我的第一个文件");
		cf.setType("txt");
		cf.setUpTime(new Date());
		this.cdFileDAO.save(cf);

		//图标列表
		List<CdIconList> iconss = this.cdIconListDAO.findAll();
		this.cdIconListDAO.delete(iconss.toArray(new CdIconList[iconss.size()]));
		CdIconList list1 = new CdIconList();
		list1.setName("图标集合1");
		this.cdIconListDAO.save(list1);

		//图标
		List<CdIcon> icons = this.cdIconDAO.findAll();
		this.cdIconDAO.delete(icons.toArray(new CdIcon[icons.size()]));
		CdIcon icon = new CdIcon();
		icon.setCode(UUID.randomUUID().toString());
		icon.setText("图标1");
		this.cdIconDAO.save(icon);

		//便签类型
		List<CdNoteType> types = this.cdNoteTypeDAO.findAll();
		this.cdNoteTypeDAO.delete(types.toArray(new CdNoteType[types.size()]));
		CdNoteType t1 = new CdNoteType();
        t1.setName("普通文本");
		t1.setWidth(1);
		t1.setHeight(1);
        t1.setCode("plain_text");
        CdNoteType t2 = new CdNoteType();
        t2.setName("日历提醒");
        t2.setWidth(1);
        t2.setHeight(1);
        t2.setCode("calendar_alert");
        CdNoteType t3 = new CdNoteType();
        t3.setName("九宫格");
        t3.setWidth(1);
        t3.setHeight(1);
        t3.setCode("grid9");
        this.cdNoteTypeDAO.save(t1, t2, t3);

		//便签
		List<CdNote> notes = this.cdNoteDAO.findAll();
		this.cdNoteDAO.delete(notes.toArray(new CdNote[notes.size()]));
		CdNote nt1 = new CdNote();
		nt1.setAlertEnabled(false);
		nt1.setDateEnableld(false);
        nt1.setType(t1);
		nt1.setTitle("第一个便签");
		nt1.setNoteJson("{}");
		this.cdNoteDAO.save(nt1);

		//伙伴口令
		List<CdShareAccount> accs = this.cdShareAccountDAO.findAll();
		this.cdShareAccountDAO.delete(accs.toArray(new CdShareAccount[accs.size()]));
		CdShareAccount sa = new CdShareAccount();
		sa.setDeadTime(new Date());
		sa.setCode(UUID.randomUUID().toString());
		sa.setAccessJson("{}");
		this.cdShareAccountDAO.save(sa);

	}

}
